package edu.xhu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


public class WebviewActivity extends Activity {
	
	private ProgressBar progress;
	private WebView webview;
	private TextView back, title;
	
	private ImageView prve, next, reload, close;
	private String reqUrl;
	
	//请求链接的key
	public static final String REQUEST_URL 		= "data";
	//转发链接
	public static final String REFERER			= "referer";
	
	private void findViews(){
		progress=(ProgressBar)findViewById(R.id.progress);
		webview=(WebView)findViewById(R.id.webview);
		back=(TextView)findViewById(R.id.back);
		title=(TextView)findViewById(R.id.title);
		prve = (ImageView) findViewById(R.id.prev);
		next = (ImageView) findViewById(R.id.next);
		reload = (ImageView) findViewById(R.id.reload);
		close = (ImageView) findViewById(R.id.close);
	}
	
	Handler handler=new Handler(){
		public void handleMessage(Message msg){
			if(msg.what==100){
				progress.setVisibility(View.GONE);
			}else{
				progress.setProgress(msg.what);
			}
		}
	};
	
	private void initWebView(){
		webview.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, final String url) {
				progress.setVisibility(View.VISIBLE);
				view.loadUrl(url);
				return true;
			}
		});

		webview.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int newProgress) {
				Message msg = new Message();
				msg.what = newProgress;
				handler.sendMessage(msg);
			}
			@Override
			public void onReceivedTitle(WebView view, String titles) {
				title.setText(titles);
				super.onReceivedTitle(view, titles);
			}
		});
		
		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setSupportZoom(true);
		webview.getSettings().setBuiltInZoomControls(true);        
		webview.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
	}
	
	private void loadData(){
		Intent intent=getIntent();
		reqUrl=intent.getStringExtra("data");
		if(reqUrl != null)
			webview.loadUrl(reqUrl);
		else 
		{
			finish();
		}
		
	}
	
	private void setListener(){
		ClickListener listener=new ClickListener();
		back.setOnClickListener(listener);
		prve.setOnClickListener(listener);
		next.setOnClickListener(listener);
		reload.setOnClickListener(listener);
		close.setOnClickListener(listener);
	}
	
	class ClickListener implements OnClickListener{
		public void onClick(View v){
			if(v==back){
				finish();
			}
			if(v == prve) {
				if(webview.canGoBack())
					webview.goBack();
			}
			if(v == next) {
				if(webview.canGoForward())
					webview.goForward();
			}
			if(v == reload) {
				webview.loadUrl(reqUrl);
			}
			if(v == close) {
				finish();
			}
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);
		
		findViews();
		initWebView();
		loadData();
		setListener();
		
	}

	@Override
	protected void onDestroy() {
		webview.setVisibility(View.GONE);
		webview.destroy();
		
		super.onDestroy();
	}
}
